﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Minecraft.Data.NBT;

namespace Minecraft.Data {
    public class Entity: ITagConverter {

        public String ID;
        public Location Pos;
        public Location Motion;
        public Orientation Rotation;
        public float FallDistance;
        public short Fire;
        public short Air;
        public Boolean OnGround;

        public Entity() { }
        public Entity(Tag data) {

            foreach (var child in data.Children) {
                switch (child.Name) {
                    case "id":
                        this.ID = (string)child.Payload;
                        break;

                    case "Pos":
                        this.Pos.X = (double)child.Children[0].Payload;
                        this.Pos.Y = (double)child.Children[1].Payload;
                        this.Pos.Z = (double)child.Children[2].Payload;
                        break;

                    case "Motion":
                        this.Motion.X = (double)child.Children[0].Payload;
                        this.Motion.Y = (double)child.Children[1].Payload;
                        this.Motion.Z = (double)child.Children[2].Payload;
                        break;

                    case "Rotation":
                        this.Rotation.Yaw = (float)child.Children[0].Payload;
                        this.Rotation.Pitch = (float)child.Children[1].Payload;
                        break;

                    case "FallDistance":
                        this.FallDistance = (float)child.Payload;
                        break;

                    case "Fire":
                        this.Fire = (short)child.Payload;
                        break;

                    case "Air":
                        this.Air = (short)child.Payload;
                        break;

                    case "OnGround":
                        this.OnGround = ((byte)child.Payload).Equals(1);
                        break;

                    default:
                        break;
                }
            }
        }

        public Tag ToTag() {
            Tag root = new Tag() { Type = TagType.Compound, Name = "Entity" };

            root.Children.Add(new Tag() { Type = TagType.String, Name = "id", Payload = ID });

            Tag pos = new Tag() { Type = TagType.List, ListType = TagType.Double, Name = "Pos" };
            pos.Children.Add(new Tag() { Type = TagType.Double, Payload = Pos.X });
            pos.Children.Add(new Tag() { Type = TagType.Double, Payload = Pos.Y });
            pos.Children.Add(new Tag() { Type = TagType.Double, Payload = Pos.Z });
            root.Children.Add(pos);

            Tag motion = new Tag() { Type = TagType.List, ListType = TagType.Double, Name = "Motion" };
            motion.Children.Add(new Tag() { Type = TagType.Double, Payload = Motion.X });
            motion.Children.Add(new Tag() { Type = TagType.Double, Payload = Motion.Y });
            motion.Children.Add(new Tag() { Type = TagType.Double, Payload = Motion.Z });
            root.Children.Add(motion);

            Tag rot = new Tag() { Type = TagType.List, ListType = TagType.Float, Name = "Rotation" };
            rot.Children.Add(new Tag() { Type = TagType.Float, Payload = Rotation.Yaw });
            rot.Children.Add(new Tag() { Type = TagType.Float, Payload = Rotation.Pitch });
            root.Children.Add(rot);

            root.Children.Add(new Tag() { Type = TagType.Float, Name = "FallDistance", Payload = FallDistance });
            root.Children.Add(new Tag() { Type = TagType.Short, Name = "Fire", Payload = Fire });
            root.Children.Add(new Tag() { Type = TagType.Short, Name = "Air", Payload = Air });
            root.Children.Add(new Tag() { Type = TagType.Byte, Name = "OnGround", Payload = Convert.ToByte(OnGround) });

            return root;
        }

    }
}
